package com.sbr.esbmonitor.servicemix;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.JMX;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class ServerInstance {

	private String jmxUrl = "service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root";
	private String userName = "smx";
	private String password = "smx";


	public void connect() throws MalformedObjectNameException, NullPointerException, IOException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
		JMXServiceURL url = null;
		try {
			url = new JMXServiceURL(jmxUrl);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		JMXConnector jmxc = null;
		HashMap<String,Object> env = new HashMap<String,Object>();
		env.put(JMXConnector.CREDENTIALS,new String[] {userName, password});
		try {
			jmxc = JMXConnectorFactory.connect(url, env);
		} catch (IOException e) {
			e.printStackTrace();
		}
		MBeanServerConnection mbsc = null;
		try {
			mbsc = jmxc.getMBeanServerConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}

		String domains[] = null;
		try {
			domains = mbsc.getDomains();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Arrays.sort(domains);
		for (String domain : domains) {
			System.out.println("\tDomain = " + domain);
		}

		ObjectName objName = new ObjectName("org.apache.camel:type=routes,*");
		List<ObjectName> cacheList = new LinkedList(mbsc.queryNames(objName, null));
		for (Iterator<ObjectName> iter = cacheList.iterator(); iter.hasNext();)
		{
			objName = iter.next();
			String keyProps = objName.getCanonicalKeyPropertyListString();
			ObjectName objectInfoName = new ObjectName("org.apache.camel:" + keyProps);
			String routeId = (String) mbsc.getAttribute(objectInfoName, "RouteId");
			String description = (String) mbsc.getAttribute(objectInfoName, "Description");
			String state = (String) mbsc.getAttribute(objectInfoName, "State");

			System.out.println("routeId : "+routeId);
			System.out.println("description : "+description);
			System.out.println("state : "+state);

			getComponents(description);

		}







	}

	private void getComponents(String description) {

		List<String> elementList = new ArrayList<String>();
		String element = getElement(description);
		while(!element.isEmpty()) {
			elementList.add(element);
			element = getElement(element);
		}
		for (String domain : elementList) {
			System.out.println("\tComposant = " + domain);
		}
		System.out.println(elementList);
	}
	
	public Collection<String> getElements(String route) {
		List<String> elementList = new ArrayList<String>();
		
		String element = route;
		while(hasMoreElement(element)) {
			element = getElement(element);
			elementList.add(element);
		}
		
		return elementList;
	}
	
	public boolean hasMoreElement(String temp) {
		return hasMoreLeftBracket(temp);
	}

	public String getElement(String route) {
		
		int rightBracketIndex = route.lastIndexOf("]");
		int leftBracketIndex = route.indexOf("[");
		
		String temp = route;
//		while (leftBracketIndex < rightBracketIndex) {
//			temp = temp.substring(leftBracketIndex, rightBracketIndex);
//		}
//		
//		return temp;
		
		System.out.println("first temp : "+temp);
		while(isNextBracketALeftOne(temp)) {
			System.out.println("temp : "+temp);
			temp = temp.substring(getNextLeftBracketIndex(temp)+1);
		}
		
		if (getNextRightBracketIndex(temp) > 0) {
			temp = temp.substring(0, getNextRightBracketIndex(temp));
		}
		
		System.out.println("final temp : "+temp);
		return temp;
		
		
	}
	
	public int getNextLeftBracketIndex(String route) {
		int leftBracketIndex = route.indexOf("[");
		System.out.println("getNextLeftBracketIndex : "+leftBracketIndex);
		return leftBracketIndex;
	}
	
	public boolean isNextBracketALeftOne(String route) {
		boolean bool =  route.indexOf("[") < route.indexOf("]");
		System.out.println("isNextBracketALeftOne : "+bool);
		return bool;
	}
	
	public boolean hasMoreLeftBracket(String route) {
		return route.contains("[");
	}
	
	public int getNextRightBracketIndex(String route) {
		return route.indexOf("]");
	}

	public static void main(String[] args) {
		ServerInstance serveur = new ServerInstance();
		try {
			serveur.connect();
		} catch (MalformedObjectNameException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AttributeNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstanceNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MBeanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ReflectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
